package com.smart.manager.member.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.ArcoTips;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.core.Page;
import com.smart.manager.member.entities.bodys.RMemberProfileMain;
import com.smart.manager.member.entities.models.MemberProfileMain;
import com.smart.manager.member.repository.MemberProfileMainRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;


/**
 * @author Administrator
 */

@Api(tags = "会员资料")
@RestController
@RequestMapping(SmartConstant.MANAGER_API_PREFIX + "member/profile/main")
@RequiredArgsConstructor
public class ProfileMainApi {
    private final MemberProfileMainRepository memberProfileMainRepository;

    @GetMapping(value = "pageList")
    @ApiOperation(value = "查询会员")
    @SaCheckPermission("member:profile:main:pageList")
    public AjaxResult<Page<MemberProfileMain>> pageList(@Validated RMemberProfileMain.SearchBody body) {
        Page<MemberProfileMain> result = memberProfileMainRepository.pageList(body);
        return AjaxResult.data(result);
    }


    @GetMapping(value = "initialization/{id}")
    @ApiOperation(value = "清除账号猪仔数据")
    @SaCheckPermission("member:profile:main:initialization")
    public AjaxResult<String> initialization(@PathVariable Long id) {
        memberProfileMainRepository.delPigInfo(id);
        return AjaxResult.success();
    }


    @PostMapping(value = "add")
    @ApiOperation(value = "新增会员")
    @SaCheckPermission("member:profile:main:add")
    public AjaxResult<String> add(@RequestBody @Validated RMemberProfileMain.EditBody body) {
        memberProfileMainRepository.add(body);
        return AjaxResult.success();
    }


    @PutMapping(value = "update/{id}")
    @ApiOperation(value = "修改会员")
    @SaCheckPermission("member:profile:main:update")
    public AjaxResult<String> update(@PathVariable Long id, @RequestBody @Validated RMemberProfileMain.EditBody body) {
        memberProfileMainRepository.update(id, body);
        return AjaxResult.success();
    }


    @DeleteMapping(value = "delete")
    @ApiOperation(value = "删除会员")
    @SaCheckPermission("member:profile:main:delete")
    public AjaxResult<Integer> delete(@RequestBody @NotEmpty(message = ArcoTips.PK_EMPTY) List<Long> ids) {
        int x = memberProfileMainRepository.delByIds(ids);
        return AjaxResult.delSuccess(x);
    }

    @PostMapping(value = "generated")
    @ApiOperation(value = "生成虚拟会员")
    @SaCheckPermission("member:profile:main:generated")
    public AjaxResult<String> add(@RequestBody @Validated RMemberProfileMain.GeneratedBody body) {
        memberProfileMainRepository.generated(body);
        return AjaxResult.success();
    }

    @PutMapping(value = "updateRemarks/{id}")
    @ApiOperation(value = "修改会员备注")
    @SaCheckPermission("member:profile:main:update:remarks")
    public AjaxResult<String> updateRemarks(@PathVariable Long id, @RequestBody @Validated RMemberProfileMain.RemarksBody body) {
        memberProfileMainRepository.updateRemarks(id, body);
        return AjaxResult.success();
    }


}
